package cn.mavor.day0910.Student;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 打印当前工作目录
        System.out.println("当前工作目录: " + System.getProperty("user.dir"));

        try {
            List<String> lines = FileUtils.readAllLines(FileUtils.getDataFile());
            StudentDataProcessor processor = new StudentDataProcessor(lines);

            // (1) 读取data.txt，输出学生的姓名和分数到文件studs.txt
            processor.writeNameAndScore();

            // (2) 选出分数最高的学生
            String highestScorer = processor.findHighestScorer();
            System.out.println("分数最高的学生: " + highestScorer);

            // (3) 计算每个班级的平均分
            processor.calculateClassAverages();

            // (4) 查询成绩
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要查询的学生姓名: ");
            String name = scanner.nextLine();
            processor.queryScore(name);

            // (5) 修改成绩
            System.out.print("请输入要修改成绩的学生姓名: ");
            String nameToUpdate = scanner.nextLine();
            System.out.print("请输入新的成绩: ");
            int newScore = scanner.nextInt();
            processor.updateScore(nameToUpdate, newScore);

            scanner.close();
        } catch (FileNotFoundException e) {
            System.err.println("文件未找到: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("文件操作时发生错误: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}